//
//  MainTbaBarController.m
//  SXH
//
//  Created by 龚啸 on 15/10/8.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "MainTbaBarController.h"
#import "MoreViewController.h"
#import "MeViewController.h"
#import "FindViewController.h"
#import "WrapperMainViewController.h"
#import "HWNavigationController.h"
#import "MainViewController.h"
#import "RouteViewController.h"
#import "FindMeViewController.h"
#import "LoginViewController.h"
#import "MeNewViewController.h"
#import "LocalProductViewController.h"
#import "CommonService.h"
#import "CheckVersionManager.h"
#import "AdView.h"
#import "ADModel.h"
#import "WebViewController.h"
#import "NSDate+WQCalendarLogic.h"
#import "WrapperViewController.h"
#import "LocalProductDetailViewController.h"
#import "SDWebImageManager.h"
@interface MainTbaBarController ()<AdViewDelegate>

@property(nonatomic,strong) FindViewController  *find;
@property(nonatomic,strong) LocalProductViewController  *routeVC;
@property(nonatomic,strong) MeNewViewController *me;
@property(nonatomic,strong) AdView *adView;
@property(nonatomic,strong) NSMutableArray *adArray;


@end

@implementation MainTbaBarController
@synthesize find;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addAdView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSelectedIndex) name:@"BACK_MECENTER_NOTIFICATION" object:nil];//监听一个通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haha) name:@"test" object:nil];//监听一个通知
    // 1.初始化子控制器
    WrapperMainViewController *home = [[WrapperMainViewController alloc] init];
    
//    MainViewController *home = [[MainViewController alloc] init];
//    home.parkId = PARK_ID;
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    
    [self addChildVc:home title:@"首页" image:@"首页_02" selectedImage:@"首页_01"];

    self.routeVC = [[LocalProductViewController alloc]init];
    self.routeVC.isTabVC = YES;
    [self addChildVc:self.routeVC title:@"特产" image:@"techandark" selectedImage:@"techanlight"];
    
    
    find = [[FindViewController alloc] init];
    [self addChildVc:find title:@"发现" image:@"发现_02.png" selectedImage:@"发现_01"];
//    MeViewController *me = [[MeViewController alloc] init];
//    [self addChildVc:me title:@"我的" image:@"我_02" selectedImage:@"我_01"];
    
    self.me = [[MeNewViewController alloc] init];
//    FindMeViewController *me = [[FindMeViewController alloc]init];
//    me.backBtn.hidden = YES;
    self.me.isFromOtherVC = NO;
    [self addChildVc:self.me title:@"我" image:@"我_02" selectedImage:@"我_01"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadNewNumber) name:Notification_findmeVC_refresh object:nil];
    [CheckVersionManager sharedCheckVersionManager];
//    MoreViewController *more = [[MoreViewController alloc] init];
//    [self addChildVc:more title:@"更多" image:@"更多_02" selectedImage:@"更多_01"];
    
    self.delegate = self;
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.612 green:0.612 blue:0.612 alpha:1.00];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.361 green:0.800 blue:0.271 alpha:1.00] ;
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    HWNavigationController *nav = [[HWNavigationController alloc] initWithRootViewController:childVc];
    // 添加为子控制器
    [self addChildViewController:nav];
    childVc.navigationController.navigationBarHidden = YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    if (self.selectedIndex == 1) {
        NSString *differentCity = [[NSUserDefaults standardUserDefaults] valueForKey:SelectedCityDifferent];
        self.routeVC.cityid = [[NSUserDefaults standardUserDefaults] valueForKey:SelectedCityId];
        if ([differentCity isEqualToString:@"1"] == YES) {
            [self.routeVC getLPData:1 RefreshView:nil];
        }
    }
    if (self.selectedIndex == 2) {
        find.parkId = @"";
        NSString *differentCity = [[NSUserDefaults standardUserDefaults] valueForKey:SelectedCityDifferent];
        if ([differentCity isEqualToString:@"1"] == YES) {
//            [self.routeVC.mutableArrayRouteProduce removeAllObjects];
//            [self.routeVC getRouteData:1 RefreshView:nil];
            [find.mutableArrayFind removeAllObjects];
            [find getFindData:1 RefreshView:nil];
        }
    }
    if (self.selectedIndex == 3) {
        UserEntity *entity = [UserEntity sharedInstance];
        if (entity.login == YES) {
                    MeNewViewController *vc = [[MeNewViewController alloc] init];
                    vc.userId = entity.userId;
//                    [self.navigationController pushViewController:vc animated:YES];
                        } else {
            LoginViewController *vc = [[LoginViewController alloc] init];
            vc.meVC = self;
//            if (mainVC == nil) {
//                vc.wrapperMainVC = wrapperMainVC;
                [self.navigationController pushViewController:vc animated:YES];
//            } else { 
//                vc.mainVC = mainVC;
//                [self.navigationController pushViewController:vc animated:YES];
//            }
        }
    }
}

- (void)addAdView
{
    self.adArray = [[NSMutableArray alloc]init];
    CommonService *service = [[CommonService alloc]init];
//    NSDictionary * parm = @{};
    [service getNetWorkData:nil Path:ACTIVITY Successed:^(id entity) {
        self.adArray = [ADModel arrayOfModelsFromDictionaries:[entity objectForKey:@"data"]];
        ADModel *model = [self.adArray firstObject];
//        NSString *lastDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"lastDate"];
        NSString *lastTitle = [[NSUserDefaults standardUserDefaults]objectForKey:@"lastTitle"];
        if (![model.title isEqualToString:lastTitle] ) {
            self.adView = [[AdView alloc]initWithArray:self.adArray];
            [self.view addSubview:self.adView];
            [self.adView show];
            self.adView.delegate = self;
            [[NSUserDefaults standardUserDefaults]setObject:model.title forKey:@"lastTitle"];
//            [[NSUserDefaults standardUserDefaults]setObject:[[NSDate new]stringFromDate:[NSDate new]] forKey:@"lastDate"];
            
        }
    } Failed:^(NSInteger errorCode, NSString *message) {
        
    }];
}

//- (void)haha
//{
//    [self.view addSubview:self.adView];
//    [self.adView show];
//
//}

- (void)ClickAtModel:(ADModel *)model
{
    switch ([model.target_class intValue]) {
        case 1:
        {
            WebViewController * mvc = [[WebViewController alloc]init];
            mvc.url = model.website;
            mvc.vcTitle = model.title;
            mvc.contentShare = model.introduce;
            [[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:IMAGE_STRING(model.pic_thumb)] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                mvc.imageShare = image;}];

            [[self.childViewControllers firstObject] pushViewController:mvc animated:YES];
        }
            break;
        case 2:
        {
            WrapperViewController * mvc = [[WrapperViewController alloc]init];
            ParkEntity *parkentity = [[ParkEntity alloc]init];
            parkentity.ID = model.target_id;
            mvc.parkEntity = parkentity;
            [[self.childViewControllers firstObject] pushViewController:mvc animated:YES];

        }
            break;
        case 3:
        {
            LocalProductDetailViewController *mvc = [[LocalProductDetailViewController alloc]init];
            LPEntity *entity = [[LPEntity alloc]init];
            entity.ID = model.target_id;
            mvc.wrapEntity = entity;
            [[self.childViewControllers firstObject] pushViewController:mvc animated:YES];

        }
            break;
            
        default:
            break;
    }

}

-(void) dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];//移除通知
}



-(void)changeSelectedIndex{
    self.selectedIndex = 3;
}

- (void)reloadNewNumber
{
    if ([[CheckVersionManager sharedCheckVersionManager]getNewPackageNumber] > 0) {
        self.me.tabBarItem.badgeValue = [NSString stringWithFormat:@"%zi",[[CheckVersionManager sharedCheckVersionManager]getNewPackageNumber]];
    }
    else
    {
        self.me.tabBarItem.badgeValue = nil;
    }
}

@end
